// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ccube.h"
#include <soil.h>
#pragma comment(lib,"SOIL.lib")

#define MOVE_STEP .4
#define ANGLE_STEP 0.8
#define PI 3.1416f

float g_x=0.0f;
float g_y=0.0f;
float g_z=10.0f;
float g_roll= 0.0f;
float g_pitch= 0.0f;
float g_yaw= 0.0f;
int g_w;
int g_h;
float g_cubeAngle= 0.f;
CCube g_cubo;
CCube g_cubo2;

void Keyboard(unsigned char key,int x, int y)
{
	//keyboard callback function
	switch (key)
	{
	case '8':	g_x-= MOVE_STEP*sin(g_yaw*PI/180);
				g_z-= MOVE_STEP*cos(g_yaw*PI/180);break;
	case '2':	g_x+= MOVE_STEP*sin(g_yaw*PI/180);
				g_z+= MOVE_STEP*cos(g_yaw*PI/180);break;
	case '6': g_yaw-= ANGLE_STEP; break;
	case '4': g_yaw+= ANGLE_STEP; break;
	case 27: exit(0);
	}
}

void Set3DView()
{
	//set projection matrix
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

	glEnable(GL_DEPTH_TEST);

	GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 0.1 };
	GLfloat light_diffuse[] = { 0.6,0.6,0.6,1.0};


	//set modelview matrix
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();

	glEnable (GL_LIGHTING);
	glEnable (GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, (GLfloat *)light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, (GLfloat *)light_diffuse);
	GLfloat light_spot_direction[] = { -1.0, -1.0, -1.0 };
	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, (GLfloat *)light_spot_direction);
	glRotatef(-g_roll, 0.0, 0.0, 1.0);	
	glRotatef(-g_yaw, 0.0, 1.0, 0.0);
	glRotatef(-g_pitch, 1.0, 0.0, 0.0);	
	glTranslatef(-g_x, -g_y, -g_z);

}





//int texturaL;

void DrawScene(void)
{
	//clean the backbuffer
	glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//viewing transformation
	Set3DView();


	//draw the cube
	g_cubo.draw();
	g_cubo2.draw();
/*
	glBegin (GL_QUADS);
	glNormal3f(0,0,1);
	glTexCoord2f(0.0,1.0);
	glVertex3f(0,0,5);
	glTexCoord2f(1.0,1.0);
	glVertex3f(5,0,5);
	glTexCoord2f(1.0,0.0);
	glVertex3f(5,5,5);
	glTexCoord2f(0.0,0.0);
	glVertex3f(0,5,5);
	glEnd();
*/
}

void Reshape (int w, int h)
{
	//Reshape callback function
	g_h= h;
	g_w= w;
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
}

int main(int argc, char** argv)
{

	//INIT GLUT/////////////////////
	////////////////////////////////
	//init window and OpenGL context
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (1024, 768); 
	glutCreateWindow (argv[0]);
	glutFullScreen();


	//callback functions
	glutDisplayFunc(DrawScene);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);

	g_cubo.setPosition(0.0,0.5,0.0);
	g_cubo.setScale(1,1,1);
	g_cubo.setColor(0,0,1);
	g_cubo2.setPosition(0.0,-3.0,-3.0);
	g_cubo2.setScale(1,1,1);
	g_cubo2.setColor(1,0,0);

	int textura1= SOIL_load_OGL_texture( "textures/alaves.png", 0,0,0 );
	g_cubo.setTexture(textura1);
	int textura2= SOIL_load_OGL_texture( "textures/fcb.png", 0,0,0 );
	g_cubo2.setTexture(textura2);

	//texturaL = SOIL_load_OGL_texture( "textures/lagarto.png", 0,0,0 );
	//glEnable(GL_TEXTURE_2D);
	//glBindTexture(GL_TEXTURE_2D, texturaL);
	
	glDisable(GL_DEPTH_TEST);

	while (1)
	{
		//UPDATE////////////////////
		////////////////////////////
		//"move" the cube
		g_cubeAngle+= 0.1;
		//queued events?
		glutMainLoopEvent();


		//RENDER////////////////////
		////////////////////////////
		glutPostRedisplay();
		glutSwapBuffers();
	}
   return 0;
}